import 'package:flutter/material.dart';
import 'package:sainbainu/common/extension/load_image.dart';

typedef LogoChange = void Function();
typedef SearchChange = void Function();

class CustomAppBarWidget extends StatelessWidget
    implements PreferredSizeWidget {
  final TabController? tabController;

  final List<Widget>? tabs;
  final Widget? title;
  final bool showSearchIcon;

  final ValueChanged<int>? onTabTap;
  final LogoChange? onLogoTap;
  final SearchChange? onSearchTap;

  const CustomAppBarWidget({
    super.key,
    this.tabController,
    this.tabs,
    this.title,
    this.onTabTap,
    this.onLogoTap,
    this.onSearchTap,
    this.showSearchIcon = false,
  });

  @override
  Widget build(BuildContext context) {
    return AppBar(
      leadingWidth: 130,
      leading: Align(
        child: GestureDetector(
          onTap: onLogoTap,
          child: Image.asset(
            'assets/images/logo.png',
            width: 100,
          ),
        ),
      ),
      titleSpacing: 0,
      title: title ??
          TabBar(
            isScrollable: true,
            tabAlignment: (tabs ?? []).length > 2
                ? TabAlignment.start
                : TabAlignment.center,
            controller: tabController,
            dividerHeight: 0,
            onTap: onTabTap,
            tabs: tabs ?? [],
          ),
      actions: [
        showSearchIcon
            ? Row(
                children: [
                  IconButton(
                    onPressed: onSearchTap,
                    icon: Image.asset(
                      'search.png'.img,
                      width: 24,
                      height: 24,
                    ),
                  ),
                  const SizedBox(width: 6),
                ],
              )
            : const SizedBox.shrink(),
      ],
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}
